#include "BTControl.h"
#include "Ioctl.h"
#include "Wavedev.h"
#include "Config.h"
#include "SysVolume.h"

CBTControl::CBTControl(void)
{
	m_bConnectState = TRUE;
}

CBTControl::~CBTControl(void)
{

}

BOOL CBTControl::SetBluetoothBC03Mode(BOOL mode)
{
	HANDLE hDev;
	DWORD dwMode = mode;

	////RETAILMSG (APDEBUG, (TEXT("++ SetBluetoothBC03Mode dwMode =%d \r\n"),dwMode));

	hDev = CreateFile(TEXT("UTC1:"), GENERIC_READ|GENERIC_WRITE,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL, OPEN_ALWAYS, 0, 0);
	if(hDev)
	{
		DeviceIoControl(hDev, IOCTL_UTC_BT_SETBC03MODE, &dwMode, sizeof(DWORD),
			NULL, 0, NULL, NULL);
	}
	else
	{
		return FALSE;
	}

	////RETAILMSG (APDEBUG, (TEXT("-- SetBluetoothBC03Mode\r\n")));

	CloseHandle(hDev);
	return TRUE;
}

BOOL CBTControl::GetBluetoothMode(void)
{
	HANDLE hDev;
	DWORD dwMode = 0;

	////RETAILMSG (APDEBUG, (TEXT("++ SetBluetoothBC03Mode dwMode =%d \r\n"),dwMode));

	hDev = CreateFile(TEXT("UTC1:"), GENERIC_READ|GENERIC_WRITE,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL, OPEN_ALWAYS, 0, 0);
	if(hDev)
	{
		DeviceIoControl(hDev, IOCTL_UTC_BT_GETBTMODE, NULL, 0,
			&dwMode, sizeof(DWORD), NULL, NULL);
	}
	else
	{
		return FALSE;
	}

	////RETAILMSG (APDEBUG, (TEXT("-- SetBluetoothBC03Mode\r\n")));

	CloseHandle(hDev);

	return dwMode;
}

BOOL CBTControl::SetBluetoothAudioMode(BOOL bMode)
{
	MMDRV_MESSAGE_PARAMS InBuf;
	DWORD OutBuf;
	HANDLE hDev;

	////RETAILMSG (APDEBUG, (TEXT("++ SetBluetoothAudioMode bMode =%d \r\n"),bMode));

	hDev = CreateFile(TEXT("WAV1:"), GENERIC_READ|GENERIC_WRITE,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL, OPEN_ALWAYS, 0, 0);

	if(INVALID_HANDLE_VALUE == hDev)
	{
		return FALSE;
	}

	if(bMode)
	{
		InBuf.uMsg = WODM_BTSTART;
	}
	else
	{
		InBuf.uMsg = WODM_BTSTOP;
	}

	InBuf.uDeviceId = 0;
	InBuf.dwUser = 0;
	InBuf.dwParam1 = 0;
	InBuf.dwParam2 = 0;

	if(MMSYSERR_NOERROR != DeviceIoControl(hDev, IOCTL_WAV_MESSAGE, &InBuf, sizeof(InBuf), 
		&OutBuf,sizeof(OutBuf), NULL, NULL))
	{
		//return FALSE;
	}

	CSysVolume vol;

	if(bMode)
	{
		vol.SetVolumeScreenTap(VOL_MUTE);
		vol.SetVolume(0);
	}
	else
	{
		vol.SetVolumeScreenTap((VolumeModeType)CConfig::GetInstance()->GetTabSound());
		vol.SetVolume(CConfig::GetInstance()->GetVolume());
	}

	////RETAILMSG (APDEBUG, (TEXT("-- SetBluetoothAudioMode bMode =%d \r\n"),bMode));

	CloseHandle(hDev);
	return TRUE;
}

BOOL CBTControl::GetBluetoothAudioMode(void)
{
	////RETAILMSG (APDEBUG, (TEXT("++ GetBluetoothAudioMode\r\n")));
	MMDRV_MESSAGE_PARAMS InBuf;
	DWORD OutBuf;
	HANDLE hDev;

	hDev = CreateFile(TEXT("WAV1:"), GENERIC_READ|GENERIC_WRITE,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL, OPEN_ALWAYS, 0, 0);

	if(INVALID_HANDLE_VALUE == hDev)
	{
		return FALSE;
	}

	InBuf.uMsg = WODM_BTSTATE;
	InBuf.uDeviceId = 0;
	InBuf.dwUser = 0;
	InBuf.dwParam1 = 0;
	InBuf.dwParam2 = 0;

	if(MMSYSERR_NOERROR != DeviceIoControl(hDev, IOCTL_WAV_MESSAGE, &InBuf, sizeof(InBuf), 
		&OutBuf,sizeof(OutBuf), NULL, NULL))
	{
		return FALSE;
	}
	
	CloseHandle(hDev);

	return OutBuf;

	////RETAILMSG (APDEBUG, (TEXT("-- GetBluetoothAudioMode\r\n")));
}

BOOL CBTControl::SetSpeakerVol(int nVol)
{
	return TRUE;
}

BOOL CBTControl::GetSpeakerVol(int &nVol)
{
	return TRUE;
}

BOOL CBTControl::SetMicrophoneVol(int nVol)
{
	return TRUE;
}

BOOL CBTControl::GetMicrophoneVol(int &nVol)
{
	return TRUE;
}

void CBTControl::SetConnectState(BOOL bState)
{
	m_bConnectState = bState;
}

BOOL CBTControl::IsBtConnected()
{
	return m_bConnectState;
}